#include "qxmms.h"

void qxmms(query_s *q) {
	q->pos = xmms_remote_get_playlist_pos(XMMS_SESSION);
	q->time = xmms_remote_get_output_time(XMMS_SESSION);
	q->title = xmms_remote_get_playlist_title(XMMS_SESSION,q->pos);
	q->file = xmms_remote_get_playlist_file(XMMS_SESSION,q->pos);
	q->length = xmms_remote_get_playlist_time(XMMS_SESSION,q->pos);
	xmms_remote_get_volume(XMMS_SESSION,&q->volume,&q->volume);
	xmms_remote_get_info(XMMS_SESSION,&q->bitrate,&q->samplerate,&q->channels);
}

void qxmms_run_state() {
	if(xmms_remote_is_paused(XMMS_SESSION)) {
		printf("Paused");
	} else if(xmms_remote_is_playing(XMMS_SESSION)) {
		printf("Playing");
	} else {
		printf("Stopped");
	}
}

void qxmms_track_length() {
	
	int min,sec;
	double mincalc,seccalc;

	if(p_time) {
		mincalc = (((double)q.length / 60) / 1000); min = (int)mincalc;
		seccalc = ((mincalc - min)) * 60; sec = (int)seccalc;
		printf("%d:%02d",min,sec);
	} else {
		printf("%d",q.length/1000);
	}
}

void qxmms_current_time() {

	int min,sec;
	double mincalc,seccalc;

	if(p_time) {
		mincalc = (((double)q.time / 60) / 1000); min = (int)mincalc;
		seccalc = ((mincalc - min)) * 60; sec = (int)seccalc;
		printf("%d:%02d",min,sec);
	} else {
		printf("%d",q.time/1000);
	}
}

void qxmms_channels() {
	if(q.channels == 1) {
		printf("Mono");
	} else {
		printf("Stereo");
	}
}

void qxmms_playlist_pos() {
	printf("%d",(q.pos + 1));
}

void qxmms_filename() {
	printf("%s",q.file);
}

void qxmms_bitrate() {
	printf("%.0f",(double)q.bitrate/1000);
	if(r_labels) { printf("kbps"); }
}

void qxmms_samplerate() {
	printf("%.1f",(double)q.samplerate/1000);
	if(r_labels) { printf("khz"); }
}

void qxmms_title() {
	printf("%s",q.title);
}

void qxmms_volume() {
	printf("%d",q.volume);
	if(r_labels) { printf("%%"); }
}

void qxmms_version() {
	printf("QueryXMMS qxmms %s (%s)\n",PROGRAM_VER,PROGRAM_DATE);
}

void qxmms_help() {
		puts("\nUsage: qxmms -<opts> <command>");

		puts("\nPrinting Options");
		puts("  p - track number in playlist.");
		puts("  b - bitrate."); 
		puts("  s - sample rate.");
		puts("  t - track title.");
		puts("  f - track filename.");
		puts("  c - channels (Mono/Stereo).");
		puts("  l - current track length.");
		puts("  n - current position in track.");
		puts("  r - running state.");
		puts("  v - current volume.");

		puts("\nPrint Modifier Options");
		puts("  L - print labels (khz/kbps/%).");
		puts("  S - print times in seconds.");
		puts("  Q - print quotes around each string.");

		puts("\nOther Options");
		puts("  h - display this help.");
		puts("  V - print program version.");

		puts("\nCommands");
		puts("  play - start playback (play/pause).");
		puts("  pause - pause playback.");
		puts("  stop - stop playback.");
		puts("  next - next track in playlist.");
		puts("  prev - previous track in playlist.");
		puts("  queue <filename> - add file to end of playlist.");
		puts("  volume <0-100> - set volume (in percent).");
		puts("  volume <up|down> - increment volume 5%.");
		puts("  volume <+|-><value> - increment by value");
		puts("  jump <#> - jump to number in playlist.");
		puts("  seek <min>:<sec> - jump to time in current tune.");
		puts("  seek <forward|back> <#> - seek in direction # seconds. (default 5)");
		puts("  skin <path to skin> - change xmms skin on the fly.");

		puts("\nIf no options are passed, only the current track title is printed.");
		puts("Options may be passed in any order and combonation. The order you");
		puts("pass options is the order the output is printed. Commands are");
		puts("executed before XMMS is queried.");
		
		puts("\nWritten by Bob 'tIKi_mAn' Majdak Jr.");
		puts("Released under the GNU GPL licence.");
}
